package com.example.mytest

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment

class TestDialog : DialogFragment() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_FRAME, R.style.CustomDialog)
    }

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_test, null, false)
    }


    override fun onStart() {
        super.onStart()
        val win = dialog!!.window
        // 一定要设置Background，如果不设置，window属性设置无效
        // 一定要设置Background，如果不设置，window属性设置无效
        win!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val dm = DisplayMetrics()
        activity!!.windowManager.defaultDisplay.getMetrics(dm)

        val display = activity!!.windowManager.defaultDisplay

        val params = win!!.attributes
//        params.gravity = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;
        //        params.gravity = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;
        params.gravity = Gravity.CENTER
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
//        params.width =  ViewGroup.LayoutParams.MATCH_PARENT;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
//        params.width =  ViewGroup.LayoutParams.MATCH_PARENT;|
        params.width = display.width
        params.height = display.height
        win.attributes = params


    }

}